package com.ruoyi.vplat.websocket.listener.edge;

import java.util.HashMap;

import javax.websocket.Session;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.event.NetworkEvent;
import com.ruoyi.common.websocket.handler.AbstractWebsocketMsgHandler;
import com.ruoyi.vplat.websocket.listener.AbstractWsCloseListener;

@Component
public class EdgeWsCloseListener extends AbstractWsCloseListener {
	@Autowired
	private ApplicationEventPublisher eventPublisher;
	@Override
	public void onClose(String username, Session session) {
		gatewayService.down(username);
		JSONObject res = AbstractWebsocketMsgHandler.getCommandTemplate("90003", new HashMap<String, Object>() {
			{
				put("protocol", 10000);
				put("seqNum", AbstractWebsocketMsgHandler.getRandomNum(9));
				put("terminateId", username);
			}
		});
		// 统一交给ruoyi-network-server模块处理,方便代码维护
		eventPublisher.publishEvent(new NetworkEvent(this, res.toString()));
	}

}
